package com.ftg.springbase;

import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;

/**
 * 类容器
 *
 * @author 布衣
 */
public class BeanContainer {

    Map<Class,Object> container = new HashMap<>();

    public void registerBean(Class... o){

        for (Class aClass : o) {
            try {
                Object obj = aClass.newInstance();
                //Field 反射的是属性
                Field[] declaredFields = aClass.getDeclaredFields();
                for (Field df : declaredFields) {
                    ValueForMe annotation = df.getAnnotation(ValueForMe.class);
                   if(annotation != null){
                       df.setAccessible(true);
                       String value =  annotation.value();
                       df.set(obj,value);
                   }
                }
                container.put(aClass,obj);
            } catch (InstantiationException | IllegalAccessException e) {
                throw new RuntimeException(e);
            }
        }
    }

    public <E> E getBean(Class<E> c){
        return (E)container.get(c);
    }

}
